package com.chuanyang.driverh.mainorder;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class UnLoadOverGoodsActivity extends BaseTitleActivity {
    private ImageView iv_unloadovergood_check;
    private boolean isorder;
    private int id;
    @Override
    protected int getContentResId() {
        return R.layout.order_act_unloadovergoods;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        iv_unloadovergood_check = (ImageView) findViewById(R.id.iv_unloadovergood_check);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("货主收货");
        getRightTextView().setText("");
        Intent intent = getIntent();
        if (intent.getIntExtra("orderId", -1) != -1)
        {
            isorder = true;
            id = intent.getIntExtra("orderId", -1);
        }
        else {
            isorder = false;
            id = intent.getIntExtra("waybillId", -1);
        }
    }

    @Override
    protected void initListener() {
        iv_unloadovergood_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_unloadovergood_check.setSelected(!iv_unloadovergood_check.isSelected());
                if (iv_unloadovergood_check.isSelected())
                {
                    getRightTextView().setText("确认");
                }
            }
        });

    }

    /**
     * 卸货完成 （传入订单id或者运单id）
     */
    private void updateWaybillUnload(final Integer id) {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        if (isorder) {
            params.put("orderId", id+"");
        }
        else {
            params.put("waybillId", id+"");
        }
        params.put("waybillStatus", "7");

        MyHttpUtil.updateWaybillStatus(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(activityContext,"您已替货主确认收货");
                    finish();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }

    @Override
    protected void baseGoEnsure() {
        if (iv_unloadovergood_check.isSelected())
        {
            updateWaybillUnload(id);
        }

    }
}
